<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Manage faction channels' );

$query = "SELECT access, faction_id " .
		"FROM faction_member " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
if( isset( $row[ 'faction_id' ] ) && $row[ 'access' ] >= 10 ) {
	$faction_id = $row[ 'faction_id' ];
?>	
					<div class="blok">
<?php
	printDiplomacyTab();
?>
					</div>
					<br />
<?php
	if( isset( $_SESSION[ 'channel_update_status' ] ) ) {
		print( "<div class=\"blok\">" . $_SESSION[ 'channel_update_status' ] . "</div><br />\r\n" );
		unset( $_SESSION[ 'channel_update_status' ] );
	}
	$query = "SELECT * " .
			"FROM chat_channels " .
			"WHERE group_id = $faction_id " .
			"ORDER BY name ASC";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );
	$i = 0;
?>
    				<div class="blok">
						<strong><?php transprint( 'Faction chat channels' ); ?></strong><br />
<?php
	if( $num > 0 ) {
?>
						<ul>
<?php
		while ( $i < $num ) {
			print( "<li>" . mysql_result( $result, $i, "name" ) . " - <a href=\"manage_faction_channels.php?id=" . mysql_result( $result, $i, "channel_id" ) . "\">" . translate( 'Edit' ) . "</a> - <a href=\"remove_chat_channel.php?channel_id=" . mysql_result( $result, $i, "channel_id" ) . "\">" . translate( "Remove" ) . "</a>" . "</li>" );
			$i ++;
		}
?>
						</ul>
						<?php transprint( 'Note: Removing a channel removes all messages in it too.' ); ?><br />
<?php
	}
	else {
?>
						<?php transprint( 'Your faction does not have any chat channels.' ); ?><br />
<?php
	}		
		
	$channel_id = -1;
	if( isset( $_GET[ 'id' ] ) && is_numeric( $_GET[ 'id' ] ) ) {
		$channel_id = $_GET[ 'id' ];
	}
?>
						</ul>
						<br />
<?php
	$continue = true;
	if( $channel_id == -1 ) {
		$query = "SELECT count(*) " .
				"FROM chat_channels " .
				"WHERE group_id = " . $faction_id;
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		if( $row[ 'count(*)' ] < 5 ) {
?>
						<strong><?php transprint( 'Add new channel' ); ?></strong><br />
<?php
		}
		else {
			$continue = false;
		} 
	}
	else {
?>
						<strong><?php transprint( 'Edit channel' ); ?></strong><br />
<?php 
	}
	if( $continue ) {
		$shoutbox->print_chat_channel_form( 'update_chat_channel.php', $faction_id, $channel_id );
	}
?>
						<br />
						<?php transprint( '<strong>Note: </strong> You can have a maximum of 5 chat channels.' ); ?><br />
					</div>
<?php 
}
else {
	print "<div class=\"blok\">\r\n";
	transprint( "You do not have the sufficient access level to see this page." );
	print "</div>\r\n";
}
aos_footer();
?>
